import axios from 'axios'
import route from '../router/index'
import { Loading} from 'element-ui'
import {DEV_MOON_TOKEN} from "@/constant/ConStant";
import router from "../router/index";
// axios 配置



// 响应时间5分钟
axios.defaults.timeout = 5 * 60 * 1000
// 接口地址
axios.defaults.baseURL = 'http://localhost:8808/api'
// 配置请求头
axios.defaults.headers.post['Access-Control-Allow-Origin'] = '*'
// post请求头
let loading = null;
let this_ = this
// http request 拦截器
axios.interceptors.request.use(config => {
 // if(config.headers['Content-Type']!=='multipart/form-data') {
 //    config.headers['Content-Type'] = 'application/json'
 //  }

  config.headers['Access-Control-Allow-Origin'] = '*'

    loading =  Loading.service({  //增加loading页面效果
        lock:true,  //是否显示动画
        text:'loading',
        background:'rgba(0,0,0,0,7)'
    })


    if (localStorage.getItem(DEV_MOON_TOKEN)  ) {
      config.headers[DEV_MOON_TOKEN] =localStorage.getItem(DEV_MOON_TOKEN)
  }

  return config
})

// http response 拦截器
axios.interceptors.response.use(
  response => {
      console.log(response['data'])

      if(loading!=null){
          loading.close()
      }

      if(response['data'] == null||response['data']['data'] ==null){
          return response
      }
      if( response['data']['data']['statusCode']!= null&&response['data']['data']['statusCode'] == 400){
          console.log('you not login in')
          router.push( "/login").then(r =>{

              console.log("to login page")
          }).catch(e=>{
              console.log("error login")
          })



      }
      // console.log("response:" + JSON.stringify(response))
    return response
  },
  error => {
    console.log('-----------------------------')
      return Promise.reject(error)
  }
)

/**
* get方法，对应get请求
*/


export default axios
